package cn.ecnu.controller;

import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.ConsultantMapper;
import cn.ecnu.mapper.DutyMapper;
import cn.ecnu.mapper.SupervisorMapper;
import cn.ecnu.mapper.UserMapper;
import cn.ecnu.pojo.Duty;
import cn.ecnu.pojo.User;
import cn.ecnu.service.DutyService;
import cn.ecnu.vo.Converter;
import cn.ecnu.vo.fromFront.AddDutyVo;
import cn.ecnu.vo.fromFront.ChangeDefaultDutyVo;
import cn.ecnu.vo.fromFront.DeleteDutyVo;
import cn.ecnu.vo.toFront.ConsultantVo;
import cn.ecnu.vo.toFront.SupervisorVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/duty")
public class DutyController {

    @Resource
    private DutyMapper dutyMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DutyService dutyService;

    @Resource
    private Converter converter;

    @GetMapping("/getDutyByUid/{id}/{year}/{month}")
    public R getDutyByUid(@PathVariable("id") Integer id,
                          @PathVariable("year") Integer year,
                          @PathVariable("month") Integer month) {
        try {
            List<Duty> dutyList = dutyMapper.getDutyByUid(id, year, month);
            return R.ok().put("data", dutyList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @GetMapping("/getDutyByDay/{year}/{month}/{day}")
    public R getDutyByDay(@PathVariable("year") Integer year,
                          @PathVariable("month") Integer month,
                          @PathVariable("day") Integer day) {

        try {

            List<Duty> dutyList = dutyMapper.getDutyByDay(year, month, day);

            HashMap<Object, Object> map = new HashMap<>();

            ArrayList<HashMap<String, Object>> consultants = new ArrayList<>();
            ArrayList<HashMap<String, Object>> supervisors = new ArrayList<>();

            for (Duty duty : dutyList) {
                Integer userId = duty.getUserId();
                User user = userMapper.getUserById(userId);
                Integer role = user.getRole();
                //咨询师
                if (role.equals(1)) {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("dutyId", duty.getDutyId());
                    hashMap.put("info", converter.getConsultantVo(userId));
                    consultants.add(hashMap);
                }
                //督导
                else if (role.equals(2)) {
                    HashMap<String, Object> hashMap = new HashMap<>();
                    hashMap.put("dutyId", duty.getDutyId());
                    hashMap.put("info", converter.getSupervisorVo(userId));
                    supervisors.add(hashMap);
                }
            }

            map.put("consultants", consultants);
            map.put("supervisors", supervisors);

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    @GetMapping("/getDutySummary/{year}/{month}")
    public R getDutySummary(@PathVariable("year") Integer year,
                            @PathVariable("month") Integer month) {

        try {

            List<Duty> dutyList = dutyMapper.getDutyByMonth(year, month);

            HashMap<String, HashMap<String, Integer>> map = new HashMap<>();

            //获取这个月的天数
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month + "-1"));
            int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            for (int day = 1; day <= dayOfMonth; day++) {
                HashMap<String, Integer> temp = new HashMap<>();
                temp.put("consultantCount", 0);
                temp.put("supervisorCount", 0);
                map.put("day" + day, temp);
            }

            for (Duty duty : dutyList) {
                //第几天
                Date dutyDate = duty.getDutyDate();
                calendar.setTime(dutyDate);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                //得到某天对应的 map
                String key = "day" + day;
                HashMap<String, Integer> dayMap = map.get(key);
                //判断是咨询师还是督导
                User user = userMapper.getUserById(duty.getUserId());
                Integer role = user.getRole();
                if (role.equals(1)) {
                    dayMap.put("consultantCount", dayMap.get("consultantCount") + 1);
                } else if (role.equals(2)) {
                    dayMap.put("supervisorCount", dayMap.get("supervisorCount") + 1);
                }
            }

            return R.ok().put("data", map);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return R.error();

    }

    @PostMapping("/deleteDuty")
    public R deleteDuty(@RequestBody DeleteDutyVo vo) {
        dutyService.deleteDuty(vo);
        return R.ok();
    }

    @PostMapping("/addDuty")
    public R addDuty(@RequestBody AddDutyVo vo) {
        dutyService.addDuty(vo);
        return R.ok();
    }

    @PostMapping("/createDefault/{year}/{month}")
    public R createDefault(@PathVariable("year") int year, @PathVariable("month") int month) {
        dutyService.createDefaultDuty(year, month);
        return R.ok();
    }

//    @PostMapping("/addDefaultDuty")
//    public R addDefaultDuty(@RequestBody ChangeDefaultDutyVo vo) {
//        dutyService.addDefaultDuty(vo);
//        return R.ok();
//    }


}










